package com.se7en.ufutil;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Spinner;
import android.widget.TextView;

public class FloorDisplay extends Activity implements OnClickListener {
	private String bldg;
	Button b0;
	Button b1;
	Button b2;
	Button b3;
	Button b4;
	Button clicksearch;
	Button next;
	Button close;
	Button restrm;
	RadioButton men;
	RadioButton women;
	ImageButton search;
	RadioGroup restroom;
	TextView textBldg;
	TextView textRm;
	ImageView imgVw;
	TextView ftxt;
	ImageView marker;
	WebView wv;
	String selBldg;
	String selRoom;
	Spinner s;
	private int totalfloors;	
	String room;
	int floor;
	int roomX;
	int roomY;
	protected Object[] rrs;
	private int rrpointer;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.floorplan);
    	ftxt = (TextView) findViewById(R.id.textView1);
    	
    	wv = (WebView) findViewById(R.id.webView1);
    	WebSettings webSettings = wv.getSettings();
    	webSettings.setJavaScriptEnabled(true);
    	webSettings.setBuiltInZoomControls(true);
    	
    	b0 = (Button) findViewById(R.id.button0);
		b1 = (Button) findViewById(R.id.button1);
		b2 = (Button) findViewById(R.id.button2);
		b3 = (Button) findViewById(R.id.button3);
		b4 = (Button) findViewById(R.id.button4);
		restrm = (Button) findViewById(R.id.restrm);
		clicksearch = (Button) findViewById(R.id.search);
		search = (ImageButton) findViewById(R.id.buttonSearch); 
		s = (Spinner) findViewById(R.id.textBldg);
		textRm = (TextView) findViewById(R.id.textRm);
		next = (Button) findViewById(R.id.next);
		close = (Button) findViewById(R.id.close);
		men = (RadioButton) findViewById(R.id.men);
		women = (RadioButton) findViewById(R.id.women);
		restroom = (RadioGroup) findViewById(R.id.restroom);
		
		search.setOnClickListener(this);
		clicksearch.setOnClickListener(this);
		
		b0.setOnClickListener(this);
		b1.setOnClickListener(this);
		b2.setOnClickListener(this);
		b3.setOnClickListener(this);
		b4.setOnClickListener(this);
		close.setOnClickListener(this);
		next.setOnClickListener(this);
		restrm.setOnClickListener(this);

		restroom.setOnCheckedChangeListener((new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup arg0, int checkedID) {
				String gender;
				if (checkedID==men.getId()) 
					gender = "MEN";
				else
					gender="WOMEN";
				rrs=DataAccess.getRestrooms(bldg, floor, gender);
				showRestroom(false);
			}
		}));
		
		class BldgSelectedListener implements OnItemSelectedListener {
	
		    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
		        selBldg=parent.getItemAtPosition(pos).toString();
		        textRm.setText("");
		        textRm.requestFocus();
		    }
	
		    public void onNothingSelected(AdapterView<?> parent) {
		        // Do nothing.
		    }
		}
		s.setAdapter(DataAccess.getBldgAdapter(this.getBaseContext()));
		s.setOnItemSelectedListener(new BldgSelectedListener());
			
		Bundle extras = getIntent().getExtras();
		
		ArrayAdapter<String> myAdap = (ArrayAdapter<String>) s.getAdapter(); //cast to an ArrayAdapter
		int spinnerPosition = myAdap.getPosition(extras.getString("name"));

		//set the default according to value
		s.setSelection(spinnerPosition);
		bldg=extras.getString("name");
		selBldg=bldg;
		selRoom = extras.getString("room");
	    changeRoom(selRoom,bldg);
    }
    
    
    private void setupButtons() {
  	  	s.setVisibility(View.GONE);
  	  	textRm.setVisibility(View.GONE);
  	  	clicksearch.setVisibility(View.GONE);
  	  	totalfloors=DataAccess.getFloors(bldg);
		if (totalfloors < 4) {
			b4.setVisibility(View.GONE);
		}
		else {
			b4.setVisibility(View.VISIBLE);
		}
		
		if (totalfloors < 3) {
			b3.setVisibility(View.GONE);
		}
		else {
			b3.setVisibility(View.VISIBLE);
		}
		
		if (totalfloors < 2){
			b2.setVisibility(View.GONE);
		}
		else {
			b2.setVisibility(View.VISIBLE);
		}
		b1.setVisibility(View.VISIBLE);
    }
    
    public void changeRoom(String room, String chbldg) {
    	if (room==null) {
    		roomX=0;
    		roomY=0;
    		floor=1;
    		setupButtons();
    		bldg=chbldg;
    		selBldg=chbldg;
    	}
    	else if (chbldg==null) {
        	restroom.clearCheck();
        	next.setVisibility(View.GONE);
    		roomX=0;
    		roomY=0;
    		floor=Integer.parseInt(room);
    	}
    	else {
        	int[] loc=DataAccess.getRoomLocation(room,bldg);
        	if (loc[0]==-1) {
        		//room not found
        		textRm.setText("Not found");
        		((EditText) textRm).selectAll();
        		return;
        	}
        	else {
        		
        	}
        	roomX=loc[0];
        	roomY=loc[1]-25;
        	floor=loc[2];
        	setupButtons();
        	
    	}
    	if (bldg != null && bldg.equals("Turlington")) {
    		b0.setText("Basement");
    		b0.setVisibility(View.VISIBLE);
    	}
    	else if (bldg != null && bldg.equals("FLA Gym")) {
    		b0.setText("Ground");
    		b0.setVisibility(View.VISIBLE);
    	}
    	else b0.setVisibility(View.GONE);
    	String dispfloor = "Floor "+floor;
    	if (dispfloor.equals("Floor 0") && bldg.equals("Turlington")) dispfloor="Basement";
    	else if (dispfloor.equals("Floor 0") && bldg.equals("FLA Gym")) dispfloor="Ground Floor";
    	ftxt.setText(bldg+": "+dispfloor);
		String dbldg="bp_"+bldg+"_"+floor;

    	drawXY(roomX, roomY, dbldg);

    	
    }
    
    public void drawXY(int roomX, int roomY, String dbldg) {
		Display display = getWindowManager().getDefaultDisplay(); 
		int width = display.getWidth();
		int height = display.getHeight();
		int scrollX=roomX-width/2+60;
    	int scrollY=roomY-height/2+120;
    	if (scrollX<0) scrollX=0;
    	if (scrollY<0) scrollY=0;
    	String html = "<html><head><style type=\"text/css\">#x{position:absolute;width:25;height:25;zoom:1;filter:alpha(opacity=50);opacity:0.5;";
		if (roomX==0 && roomY==0) {
			html+="display:none";
		}
		else {
			html+="top:"+roomY+";left:"+roomX;
		}
    	html+="}body{margin:0}</style></head><body onload=\"javascript:window.scrollTo("+Integer.toString(scrollX)+","+Integer.toString(scrollY)+")\"><img src=\"file:///android_asset/"+dbldg+".png\"><img src=\"file:///android_asset/cross-x1.png\" id=\"x\"></body></html>";	
    	wv.loadDataWithBaseURL("fake://not/needed", html, "text/html", "utf-8", "");
    }
    public void showRestroom(boolean gnext) {
    	if (gnext==false) {
    		next.setVisibility(View.VISIBLE);
    		if(rrs.length>1) {
	    		rrpointer=0;
	    		String dbldg="bp_"+bldg+"_"+floor;
	    		drawXY((Integer)rrs[0],(Integer)rrs[1],dbldg);
	    		next.setText("1 of "+rrs.length/2+" >>");
    		}
    		else {
    			next.setText("No restrooms");
    		}
    	}
    	else if (rrs.length>1){
    		if (rrpointer<rrs.length-2) rrpointer+=2;
    		else rrpointer=0;
    		String dbldg="bp_"+bldg+"_"+floor;
    		next.setText((rrpointer/2+1)+" of "+rrs.length/2+" >>");
    		drawXY((Integer)rrs[rrpointer],(Integer)rrs[rrpointer+1],dbldg);
    	}
    }
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.search:
			if (textRm.length() > 0) {
				changeRoom(textRm.getText().toString().toUpperCase(),selBldg);
			}
			else {
				bldg=selBldg;
				changeRoom(null,selBldg);
			}
			//Hides the keyboard after search is clicked
			InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.hideSoftInputFromWindow(textRm.getWindowToken(), 0);
			break;

		case R.id.buttonSearch:
			if (s.getVisibility()==View.GONE) {
		    	  b4.setVisibility(View.GONE);
		    	  b3.setVisibility(View.GONE);
		    	  b2.setVisibility(View.GONE);
		    	  b1.setVisibility(View.GONE);
		    	  b0.setVisibility(View.GONE);
		    	  s.setVisibility(View.VISIBLE);
		    	  textRm.setVisibility(View.VISIBLE);
		    	  clicksearch.setVisibility(View.VISIBLE);
		      }
		      else {
		    	  setupButtons();
		      }
			break;
			
		case R.id.button0:
			changeRoom("0",null);
			break;
			
		case R.id.button1:
			changeRoom("1",null);
			break;
			
		case R.id.button2:
			changeRoom("2",null);
			break;
			
		case R.id.button3:
			changeRoom("3",null);
			break;
			
		case R.id.button4:
			changeRoom("4",null);
			break;
			
		case R.id.close:
			restroom.setVisibility(View.GONE);
			break;
		
		case R.id.next:
			showRestroom(true);
			break;
		
		case R.id.restrm:
			restroom.setVisibility(View.VISIBLE);
			break;
		
			
		
			
		default:
			Log.v("PAUL", "you forgot something in the onclick of floor display?");
			break;
		}
		
		
	}
}
